package controller;


import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.RoleService;
import service.UsersService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Administrator
 * @create 2020.06.18 上午 11:53
 */
@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UsersService usersService;

//    @LogData(logType = LogType.LOGIN,description = "查看所有用户")
    @ResponseBody
    @GetMapping("/list")
    public MyResult<List<Users>> layList(LayRequest layRequest, HttpSession session){
        PageInfo<Users> rolePageInfo=usersService.selectByMap(layRequest);
        List<Role> roles = roleService.selectAll();
        session.setAttribute("roles",roles);
//        System.out.println(layRequest);
        return new MyResult<>(0,rolePageInfo.getTotal(),rolePageInfo.getList());
    }
    @LogData(logType = LogType.INSERT,description = "添加用户")
    @ResponseBody
    @RequestMapping("/add")
    public MyResult insert(Users users){
        boolean flag = usersService.insert(users);
        // 重定向 "redirect:重定向的请求地址"; 通常重定向到Controller方法
        return new MyResult<>(0);//重定向到list请求
    }




    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap){
        //查询角色信息
        Users users=usersService.selectById(id);
        //将角色信息传递到页面
        modelMap.addAttribute("users",users);
        return "update.jsp";
    }
    @LogData(logType = LogType.UPDATE,description = "修改用户信息")
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(Users users){
        //todo 调用service 更新角色信息
        boolean flag= usersService.update(users);
        return new MyResult<>(0);
    }
    @LogData(logType = LogType.UPDATE,description = "修改用户状态")
    @ResponseBody
    @RequestMapping("/updateStatus")
    public MyResult updateStatus(Users users){
        System.out.println("==========================");
        boolean b = usersService.updateStatus(users);
        return new MyResult(0);
    }
    @LogData(logType = LogType.DELETE,description = "删除用户")
    @RequestMapping("/delete")
    @ResponseBody
    public MyResult delete(Integer id){
        boolean flag= usersService.delete(id);
        return new MyResult<>(0);
    }


    @RequestMapping("/toUserinfo")
public String toUserInfo(HttpSession session,ModelMap modelMap){
        Role role = roleService.selectById(((Users)session.getAttribute("admin")).getRole_id());
        modelMap.addAttribute("rolename",role);
        return "userinfo.jsp";
}
    @ResponseBody
    @RequestMapping("/password")
    public MyResult password(String pa,String pa1,HttpSession session){
        pa  = MD5.str2MD5(pa);
        pa1  = MD5.str2MD5(pa1);
        System.out.println(pa+"========================");
        Users user = (Users) session.getAttribute("admin");
        if (user.getPassword().equals(pa)){
            user.setPassword(pa1);
            boolean b = usersService.updatePassword(user);
            return new MyResult<>(0);
        }
        return new MyResult<>(1);
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping("/away")
    public String password(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:/login.jsp";
    }
}
